package com.hongxun.design.BroadCastReceivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import com.hongxun.design.BroadCastReceivers.WifiStateBroadCast.WifiBroadCastOperations;
import com.hongxun.design.MyApp;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.object_properties.Global;


/**
 * Created by zerdoor_pc on 2016/4/18.
 */
public class WifiConnectBroadCast extends BroadcastReceiver {

    private WifiBroadCastOperations operations;

    private WifiManager wifiManager;

    public WifiConnectBroadCast(WifiBroadCastOperations operations) {

        this.operations = operations;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String SSID = wifiInfo.getSSID();
                switch (networkInfo.getState()) {
                    case CONNECTED:
                        MyLog.L(MyApp.AppName + "APActivity" + "CONNECTED");
                        if (SSID.contains(Global.SSID)) {
                            operations.disPlayWifiConResult(true, wifiInfo);
                        } else {
                            operations.disPlayWifiConResult(false, wifiInfo);
                        }
                        break;
                    case CONNECTING:
                        MyLog.L(MyApp.AppName + "APActivity" + "CONNECTING");
                        break;
                    case DISCONNECTED:
                        MyLog.L(MyApp.AppName + "APActivity" + "DISCONNECTED");
                        break;
                    case DISCONNECTING:
                        MyLog.L(MyApp.AppName + "APActivity" + "DISCONNECTING");
                        break;
                    case SUSPENDED:
                        MyLog.L(MyApp.AppName + "APActivity" + "SUSPENDED");
                        break;
                    case UNKNOWN:
                        MyLog.L(MyApp.AppName + "APActivity" + "UNKNOWN");
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
